# 1. 导入必备工具包
from flask import Flask, request, render_template_string

# 2. 创建Flask应用
app = Flask(__name__)

# 3. 定义HTML界面模板
HTML_TEMPLATE = '''
<!DOCTYPE html>
<html>
<head>
    <title>个税计算器</title>
    <style>
        body { font-family: 'Microsoft YaHei', sans-serif; margin: 40px; background: #f5f5f5; }
        .container { background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); max-width: 500px; margin: 0 auto; }
        h2 { color: #2c3e50; text-align: center; }
        input, button { width: 100%; padding: 12px; margin: 8px 0; border: 1px solid #ddd; border-radius: 4px; font-size: 16px; }
        button { background: #3498db; color: white; border: none; cursor: pointer; }
        button:hover { background: #2980b9; }
        .result { margin-top: 20px; padding: 15px; background: #ecf0f1; border-left: 4px solid #2ecc71; font-weight: bold; }
    </style>
</head>
<body>
    <div class="container">
        <h2>个税计算器</h2>
        <form method="POST">
            <input type="number" step="0.01" name="salary" placeholder="请输入月收入（元）" required>
            <button type="submit">计算个税</button>
        </form>
        {% if result is not none %}
            <div class="result">{{ result }}</div>
        {% endif %}
    </div>
</body>
</html>
'''

# 4. 个税计算逻辑（简易版）
def calculate_tax(salary):
    annual_salary = salary * 12
    # 此处为简易计算逻辑，您后续可以替换为官方公式
    tax = annual_salary * 0.1 - 2520  # 一个示例算法
    return f"预计年应纳税额：{tax:.2f} 元"

# 5. 定义路由和处理函数
@app.route('/', methods=['GET', 'POST'])
def index():
    result = None
    if request.method == 'POST':
        try:
            salary = float(request.form['salary'])
            result = calculate_tax(salary)
        except:
            result = "错误：请输入有效数字"
    return render_template_string(HTML_TEMPLATE, result=result)

# 6. 启动应用
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080, debug=True)